package primary.code07_Greedy;

import java.util.*;
import java.util.stream.Collector;

/**
 * 给定若干个字符串，如何拼接才能使得拼接后的字典序最小
 *
 * @author Yudi Wang
 * @date 2021/8/4 11:20
 */
public class Code03_DicOrder {

    public static String minDicOrder(List<String> strs) {
        Collections.sort(strs, ((o1, o2) -> (o1 + o2).compareTo((o2 + o1))));
        StringBuffer sb = new StringBuffer();
        for (String str : strs) {
            sb.append(str);
        }
        return sb.toString();
    }

    public static String minDicOrderTest(List<String> strs) {
        Collections.sort(strs, (o1, o2) -> (o1 + o2).compareTo(o2 + o1));
        StringBuffer sb = new StringBuffer();
        for (String str : strs) {
            sb.append(str);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String[] strArray = {
                "zed",
                "abrc",
                "aav",
                "a",
                "ab",
                "fdg",
                "uoij"
        };
        List<String> strs = Arrays.asList(strArray);
        System.out.println("最小字典序为：" + minDicOrder(strs));
        System.out.println("最小字典序Test为：" + minDicOrderTest(strs));
    }
}
